package cn.lingyangwl.agile.auth.service.impl;

import cn.lingyangwl.agile.auth.assembly.AuthAssembly;
import cn.lingyangwl.agile.auth.model.entity.LocalUser;
import cn.lingyangwl.agile.auth.model.rqrs.AuthUserSaveReq;
import cn.lingyangwl.agile.auth.model.rqrs.AuthUserUpdateReq;
import cn.lingyangwl.agile.auth.model.rqrs.LocalUserGetReq;
import cn.lingyangwl.agile.auth.model.rqrs.UserResp;
import cn.lingyangwl.agile.auth.service.LocalUserService;
import cn.lingyangwl.agile.auth.service.UserService;
import cn.lingyangwl.agile.member.MemberApi;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserRegisterReq;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserResp;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserUpdateReq;
import cn.lingyangwl.agile.model.constants.AppType;
import cn.lingyangwl.agile.system.SystemApi;
import cn.lingyangwl.agile.system.model.rqrs.user.UserSaveReq;
import cn.lingyangwl.framework.core.http.HttpRequestServletManager;
import cn.lingyangwl.framework.core.utils.IpUtils;
import cn.lingyangwl.framework.core.utils.servlet.ServletUtils;
import cn.lingyangwl.framework.data.mate.annotations.FieldBindHandle;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.github.yitter.idgen.YitIdHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private SystemApi systemApi;
    @Resource
    private LocalUserService localUserService;
    @Resource
    private MemberApi memberApi;
    @Resource
    private AuthAssembly authAssembly;

    @Override
    public Long saveUser(AuthUserSaveReq req, AppType appType) {
        Long userId = 0L;
        if (appType == AppType.ADMIN_CONSOLE) {
            UserSaveReq userSaveReq = new UserSaveReq();
            userSaveReq.setUserName(StringUtils.defaultIfEmpty(req.getUsername(), YitIdHelper.nextId() + ""))
                    .setEmail(req.getEmail()).setPhone(req.getPhone()).setAvatar(req.getAvatar())
                    .setNickName(StringUtils.firstNonEmpty(req.getNickName(), req.getUsername(),  "默认昵称"))
                    .setSex(req.getSex())
                    .setWorkNumber(YitIdHelper.nextId() + "");
            userId =  systemApi.saveUser(userSaveReq);
        } else {
            HttpRequestServletManager request = new HttpRequestServletManager(ServletUtils.getRequestOfNonNull());
            String requestIp = IpUtils.getRequestIp(request);
            MemberUserRegisterReq registerReq = new MemberUserRegisterReq()
                    .setRegisterIp(requestIp).setAvatar(req.getAvatar())
                    .setRealName(req.getRealName()).setUserName(req.getUsername())
                    .setLastLoginIp(requestIp).setLastLoginDate(new Date())
                    .setNickName(StringUtils.firstNonEmpty(req.getNickName(), req.getUsername(),  "默认昵称"))
                    .setPhone(req.getPhone()).setSex(req.getSex()).setRegisterTerminal(req.getTerminal());
            MemberUserResp user = memberApi.registerUserIfAbsent(registerReq);
            if (Objects.isNull(user)) {
                throw new BizException("注册失败");
            }
            userId = user.getId();
        }
        return userId;
    }

    @Override
    public void updateUser(AuthUserUpdateReq req, AppType appType) {
        if (appType == AppType.ADMIN_CONSOLE) {

        } else {
            MemberUserUpdateReq updateReq = authAssembly.toMemberUserUpdateReq(req);
            memberApi.updateUser(updateReq);
        }
    }

    @Override
    @FieldBindHandle
    public UserResp getUser(String account, AppType appType) {
        LocalUserGetReq req = new LocalUserGetReq();
        req.setAccount(account);
        req.setAppType(appType.getCode());
        LocalUser user = localUserService.getUser(req);
        if (Objects.isNull(user)) {
            return null;
        }

        UserResp resp = new UserResp();
        resp.setUserId(user.getUserId());
        resp.setUserName(user.getUserName());
        return resp;
    }
}
